package com.tea.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;
import com.tea.common.core.domain.AjaxResult;
import com.tea.common.utils.StringUtils;
import com.tea.common.utils.file.FileUtils;
import com.tea.funct.domain.TeaPriceDetails;
import com.tea.funct.domain.TeaProduct;
import com.tea.funct.domain.TeaSubclass;
import com.tea.funct.domain.test.ProductInfo;
import com.tea.funct.domain.test.Root;
import com.tea.funct.domain.test.VenderDomain;
import com.tea.funct.service.ITeaPriceDetailsService;
import com.tea.funct.service.ITeaProductService;
import com.tea.funct.service.ITeaSubclassService;
import com.tea.funct.utils.domain.*;
import com.tea.funct.utils.util.PullDataUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @PACKAGE_NAME: com.tea.controller
 * @CLASSNAME: TestController
 * @DEACRIPTION:
 * @AUTHOR: GuoChen
 * @DATE: 2024/5/24
 * @WEEK: 周五
 **/
@Slf4j
@RestController
@RequestMapping("/funct")
public class TestFunctController {

    @Autowired
    private ITeaSubclassService teaSubclassService;
    @Autowired
    private ITeaProductService teaProductService;
    @Autowired
    private ITeaPriceDetailsService teaPriceDetailsService;
    @Autowired
    private PullDataUtil pullDataUtil;

    @GetMapping("/testPullProductInfo")
    public AjaxResult getAllProductInfo() {
        // https://www.yanjiuchacha.com/api/market/atProductMoney/useTheNewQueryList?productId=1466281361064734721&startDay=2024-06-05&endDay=2024-06-12
        log.info("获取所有产品信息...");
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTY4NjE3NDAsInVzZXJuYW1lIjoiMTU1MTc1Mjg3MjkifQ.UTt2E3-LYYssHUsGv9JXI113OAnr2Vl1ITNnMG3g6GA";
        String url = "https://www.yanjiuchacha.com/api/market/atProduct/useTheNewQueryPageList?pageNo={pageNum}&pageSize=15&productType={productType}&manufacturerId=&productName=&type=";
        String urlTemp = url.replace("{pageNum}", "1"); // 默认第一页
        urlTemp = urlTemp.replace("{productType}", "1"); // 默认香烟产品
        PullProductInfo pullProductInfo = (PullProductInfo) pullDataUtil.getMethodPull(urlTemp, token, PullProductInfo.class);
        List<ProductInfoRecord> list = new ArrayList<>();
        if (pullProductInfo != null) {
            ProductInfoResult result = pullProductInfo.getResult();
            if (result != null) {
                List<ProductInfoRecord> records = result.getRecords();
                list.addAll(records);
                Integer pages = result.getPages();
                for (Integer i = 2; i <= pages; i++) {
                    urlTemp = url.replace("{pageNum}", i.toString());
                    urlTemp = urlTemp.replace("{productType}", "1");
                    PullProductInfo pullProductInfo1 = (PullProductInfo) pullDataUtil.getMethodPull(urlTemp, token, PullProductInfo.class);
                    if (pullProductInfo1 != null) {
                        ProductInfoResult result1 = pullProductInfo1.getResult();
                        if (result1 != null) {
                            List<ProductInfoRecord> records1 = result1.getRecords();
                            list.addAll(records1);
                            log.info("获取到的数据为{}条", list.size());
                        }
                    }
                }
            }
        }
        log.info("获取到的数据为：{}", pullProductInfo);
        return AjaxResult.success();
    }


    @GetMapping("/getAllProductDetailInfo")
    public AjaxResult getAllProductDetailInfo() {
        // https://www.yanjiuchacha.com/api/market/atProductMoney/useTheNewQueryList?productId=1466281361064734721&startDay=2024-06-05&endDay=2024-06-12
        log.info("获取所有产品信息...");
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTY4NjE3NDAsInVzZXJuYW1lIjoiMTU1MTc1Mjg3MjkifQ.UTt2E3-LYYssHUsGv9JXI113OAnr2Vl1ITNnMG3g6GA";
        String url = "https://www.yanjiuchacha.com/api/market/atProductMoney/useTheNewQueryList?productId={productId}&startDay={startDay}&endDay={endDay}";
        String urlTemp = url.replace("{productId}", "1466281361064734721"); // 默认第一页
        urlTemp = urlTemp.replace("{startDay}", "2021-06-12"); // 开始时间
        urlTemp = urlTemp.replace("{endDay}", "2024-06-12"); // 结束时间
        PullProductDetailInfo pullProductDetailInfo = (PullProductDetailInfo) pullDataUtil.getMethodPull(urlTemp, token, PullProductDetailInfo.class);
        List<PullProductDetailInfo> list = new ArrayList<>();
        if (pullProductDetailInfo != null) {
            List<PullProductDetailResult> result = pullProductDetailInfo.getResult();
            log.info("获取到数据为：{}", result);
        }
        return AjaxResult.success();
    }

    @GetMapping("/test")
    public AjaxResult test() {
        return AjaxResult.success("不成功便成仁！");
    }

    /**
     * 入库大类别数据
     *
     * @return
     */
    @GetMapping("/dealVender")
    public AjaxResult dealVender() {
        List<TeaSubclass> list = new ArrayList<>();
        String str = "{\"result\":[{\"id\":\"1468839835829153793\",\"venderName\":\"飞天茅台\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2021-12-09\"},{\"id\":\"1468839964007084034\",\"venderName\":\"生肖茅台\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2021-12-09\"},{\"id\":\"1527212210941308929\",\"venderName\":\"汾酒\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2022-05-19\"},{\"id\":\"1527212260949995521\",\"venderName\":\"洋河\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2022-05-19\"},{\"id\":\"1527212417317842946\",\"venderName\":\"郎酒\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2022-05-19\"},{\"id\":\"1527212470015078402\",\"venderName\":\"习酒\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2022-05-19\"},{\"id\":\"1527212519088435201\",\"venderName\":\"剑南春\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2022-05-19\"},{\"id\":\"1527212567155159042\",\"venderName\":\"五粮液\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2022-05-19\"},{\"id\":\"1527212626479394818\",\"venderName\":\"泸州老窖\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2022-05-19\"},{\"id\":\"1527541342417956866\",\"venderName\":\"茅台系列\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":2,\"createTime\":\"2022-05-20\"},{\"id\":\"13333334\",\"venderName\":\"陕西\",\"venderAddress\":\"贵州镇\",\"venderPhone\":\"13636666666\",\"productType\":1,\"createTime\":\"2021-10-10\"},{\"id\":\"1450302429840797697\",\"venderName\":\"山东\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-10-19\"},{\"id\":\"1450302494437273601\",\"venderName\":\"河南\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-10-19\"},{\"id\":\"1450302595817795585\",\"venderName\":\"河北\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-10-19\"},{\"id\":\"1450302790207008770\",\"venderName\":\"内蒙\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-10-19\"},{\"id\":\"1450302822880636930\",\"venderName\":\"辽宁\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-10-19\"},{\"id\":\"1450302872125960193\",\"venderName\":\"吉林\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-10-19\"},{\"id\":\"1450708343284019202\",\"venderName\":\"黑龙江\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-10-20\"},{\"id\":\"1456195239966027778\",\"venderName\":\"湖北\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195270945157121\",\"venderName\":\"湖南\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195308832305153\",\"venderName\":\"江苏\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195339954040833\",\"venderName\":\"安徽\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195372208238593\",\"venderName\":\"江西\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195410003111937\",\"venderName\":\"浙江\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195446866849794\",\"venderName\":\"上海\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195479993462785\",\"venderName\":\"北京\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195516358078465\",\"venderName\":\"天津\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195548457086977\",\"venderName\":\"重庆\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195599711481858\",\"venderName\":\"广东\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195633182027778\",\"venderName\":\"广西\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195660059127809\",\"venderName\":\"贵州\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195698273431554\",\"venderName\":\"云南\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195733941792770\",\"venderName\":\"四川\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456195764925116417\",\"venderName\":\"甘肃\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456196083885158401\",\"venderName\":\"福建\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1456196227707842562\",\"venderName\":\"其他\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-11-04\"},{\"id\":\"1468096864871657473\",\"venderName\":\"海南\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-12-07\"},{\"id\":\"1468096901110444033\",\"venderName\":\"深圳\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-12-07\"},{\"id\":\"1468096998732869633\",\"venderName\":\"进口\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-12-07\"},{\"id\":\"1468097048833830914\",\"venderName\":\"港澳台\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-12-07\"},{\"id\":\"1468835148597964802\",\"venderName\":\"山西\",\"venderAddress\":null,\"venderPhone\":null,\"productType\":1,\"createTime\":\"2021-12-09\"}]}";
        Root bean = JSONUtil.toBean(str, Root.class);
        List<VenderDomain> result = bean.getResult();
        Date date = new Date();
        if (result != null && result.size() > 0) {
            for (VenderDomain venderDomain : result) {
                TeaSubclass nameSubclass = teaSubclassService.selectTeaSubclassByName(venderDomain.getVenderName());
                nameSubclass.setRemark(venderDomain.getId());
                log.info("结果：{}", venderDomain);
                TeaSubclass teaSubclass = new TeaSubclass();
                teaSubclass.setSubclassName(venderDomain.getVenderName());
                teaSubclass.setSubclassDesc(venderDomain.getVenderAddress());
                teaSubclass.setCategoryId(2l);
                teaSubclass.setRemark(venderDomain.getVenderPhone());
                teaSubclass.setCreateTime(date);
                teaSubclass.setUpdateTime(date);
                list.add(nameSubclass);
                teaSubclassService.updateTeaSubclass(nameSubclass);
                // list.add(teaSubclass);
            }
            if (list != null && list.size() > 0) {
                // teaSubclassService.insertTeaSubclassAll(list);
                log.info("信息插入成功！");
            }
        }
        return AjaxResult.success();
    }


    private List<ProductInfoRecord> getProductRecordsBySubclassId(String url, String token) {
        log.info("请求拉取数据：{}", url);
        // 第一次请求
        PullProductInfo pullProductInfo = (PullProductInfo) pullDataUtil.getMethodPull(url, token, PullProductInfo.class);
        if (pullProductInfo != null) {
            if (pullProductInfo.getCode() == 200) {
                ProductInfoResult result = pullProductInfo.getResult();
                if (result != null) {
                    List<ProductInfoRecord> records = result.getRecords();
                    return records;
                }
            } else {
                log.error("请求错误1：{}", pullProductInfo);
                throw new RuntimeException("请求错误!");
            }
        } else {
            log.error("请求错误2：{}", pullProductInfo);
            throw new RuntimeException("请求错误!");
        }
        return null;
    }

    /**
     * 根据小类拉取产品信息
     *
     * @return
     */
    @GetMapping("/pullProductBySubclass")
    public AjaxResult pullProductBySubclass() throws InterruptedException {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTY4NjE3NDAsInVzZXJuYW1lIjoiMTU1MTc1Mjg3MjkifQ.UTt2E3-LYYssHUsGv9JXI113OAnr2Vl1ITNnMG3g6GA";
        // https://www.yanjiuchacha.com/api/market/atProduct/useTheNewQueryPageList?pageNo=1&pageSize=15&productType=2&manufacturerId=&productName=&
        String url = "https://www.yanjiuchacha.com/api/market/atProduct/useTheNewQueryPageList?pageNo={pageNum}&pageSize=15&productType={productType}&manufacturerId={subclass}&productName=&";
        List<TeaSubclass> list = teaSubclassService.selectTeaSubclassList(null);
        List<String> errorUrlList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (TeaSubclass teaSubclass : list) {
                String urlTemp = url.replace("{pageNum}", "1"); // 默认第一页
                urlTemp = urlTemp.replace("{subclass}", teaSubclass.getRemark());
                urlTemp = urlTemp.replace("{productType}", teaSubclass.getCategoryId().toString());
                // 第一次请求
                PullProductInfo pullProductInfo = (PullProductInfo) pullDataUtil.getMethodPull(urlTemp, token, PullProductInfo.class);
                if (pullProductInfo != null) {
                    if (pullProductInfo.getCode() == 200) {
                        ProductInfoResult result = pullProductInfo.getResult();
                        Integer pages = result.getPages();
                        if (result != null) {
                            log.info("批量插入到产品表...");
                            insertProductBySubclass(teaSubclass, result.getRecords());
                        }
                        if (pages > 1) {
                            // 递归调用数据
                            for (Integer i = 2; i <= pages; ) {
                                String rUrl = url.replace("{pageNum}", i.toString()); // 默认第一页
                                rUrl = rUrl.replace("{subclass}", teaSubclass.getRemark());
                                rUrl = rUrl.replace("{productType}", teaSubclass.getCategoryId().toString());
                                try {
                                    List<ProductInfoRecord> records = this.getProductRecordsBySubclassId(rUrl, token);
                                    if (records == null) {
                                        errorUrlList.add(rUrl);
                                        log.info("请求结果为空，记录并等待10s请求下一条！");
                                        Thread.sleep(10000);
                                        // log.info("请求数据为空，则等待20s后重试！");
                                        // Thread.sleep(20000);
                                    } else {
                                        this.insertProductBySubclass(teaSubclass, records);
                                    }
                                } catch (Exception e) {
                                    errorUrlList.add(rUrl);
                                    log.info("请求报错，记录并等待10s请求下一条！");
                                    Thread.sleep(10000);
                                    // String tUrl = "https://www.yanjiuchacha.com/api/student/management/wdStudentManagement/queryByVip";
                                    // Object obj = pullDataUtil.getMethodPull(tUrl, token, RespBean.class);
                                    // Thread.sleep(50000);
                                }
                                i++;
                            }
                        }
                    } else {
                        errorUrlList.add(urlTemp);
                        log.info("请求结果为空，记录并等待10s请求下一条！");
                        Thread.sleep(10000);
                    }
                }

            }
        }
        if (errorUrlList != null && errorUrlList.size() > 0) {
            log.info("失败获取产品信息的条数为: {}", errorUrlList.size());
            this.saveErrorUrlToFile("/home/sophia/application/teaWine/pullData/productsErrorUrl.txt", errorUrlList);
        }
        return AjaxResult.success();
    }

    /**
     * 更新产品的小类id
     *
     * @return
     */
    @GetMapping("/updateSubclassIdBySubclass")
    public AjaxResult updateSubclassIdBySubclass() throws InterruptedException {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTY4NjE3NDAsInVzZXJuYW1lIjoiMTU1MTc1Mjg3MjkifQ.UTt2E3-LYYssHUsGv9JXI113OAnr2Vl1ITNnMG3g6GA";
        // https://www.yanjiuchacha.com/api/market/atProduct/useTheNewQueryPageList?pageNo=1&pageSize=15&productType=2&manufacturerId=&productName=&
        String url = "https://www.yanjiuchacha.com/api/market/atProduct/useTheNewQueryPageList?pageNo={pageNum}&pageSize=15&productType={productType}&manufacturerId={subclass}&productName=&";
        List<TeaSubclass> list = teaSubclassService.selectTeaSubclassList(null);
        List<String> errorUrlList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (TeaSubclass teaSubclass : list) {
                String urlTemp = url.replace("{pageNum}", "1"); // 默认第一页
                urlTemp = urlTemp.replace("{subclass}", teaSubclass.getRemark());
                urlTemp = urlTemp.replace("{productType}", teaSubclass.getCategoryId().toString());
                // 第一次请求
                PullProductInfo pullProductInfo = (PullProductInfo) pullDataUtil.getMethodPull(urlTemp, token, PullProductInfo.class);
                if (pullProductInfo != null) {
                    if (pullProductInfo.getCode() == 200) {
                        ProductInfoResult result = pullProductInfo.getResult();
                        Integer pages = result.getPages();
                        if (result != null) {
                            log.info("批量插入到产品表...");
                            this.updateProductSubclassBySubclass(teaSubclass, result.getRecords());
                        }
                        if (pages > 1) {
                            // 递归调用数据
                            for (Integer i = 2; i <= pages; ) {
                                String rUrl = url.replace("{pageNum}", i.toString()); // 默认第一页
                                rUrl = rUrl.replace("{subclass}", teaSubclass.getRemark());
                                rUrl = rUrl.replace("{productType}", teaSubclass.getCategoryId().toString());
                                try {
                                    List<ProductInfoRecord> records = this.getProductRecordsBySubclassId(rUrl, token);
                                    if (records == null) {
                                        errorUrlList.add(rUrl);
                                        log.info("请求结果为空，记录并等待10s请求下一条！");
                                        Thread.sleep(10000);
                                        // log.info("请求数据为空，则等待20s后重试！");
                                        // Thread.sleep(20000);
                                    } else {
                                        this.updateProductSubclassBySubclass(teaSubclass, result.getRecords());
                                    }
                                } catch (Exception e) {
                                    errorUrlList.add(rUrl);
                                    log.info("请求报错，记录并等待10s请求下一条！");
                                    Thread.sleep(10000);
                                    // String tUrl = "https://www.yanjiuchacha.com/api/student/management/wdStudentManagement/queryByVip";
                                    // Object obj = pullDataUtil.getMethodPull(tUrl, token, RespBean.class);
                                    // Thread.sleep(50000);
                                }
                                i++;
                            }
                        }
                    } else {
                        errorUrlList.add(urlTemp);
                        log.info("请求结果为空，记录并等待10s请求下一条！");
                        Thread.sleep(10000);
                    }
                }

            }
        }
        if (errorUrlList != null && errorUrlList.size() > 0) {
            log.info("失败获取产品信息的条数为: {}", errorUrlList.size());
            this.saveErrorUrlToFile("/home/sophia/application/teaWine/pullData/productsErrorUrl.txt", errorUrlList);
        }
        return AjaxResult.success();
    }

    private void insertProductBySubclass(TeaSubclass teaSubclass, List<ProductInfoRecord> records) {
        if (records != null && records.size() > 0) {
            List<TeaProduct> teaProducts = new ArrayList<>();
            for (ProductInfoRecord p : records) {
                TeaProduct teaProduct = new TeaProduct();
                teaProduct.setCategoryId(teaSubclass.getCategoryId());
                teaProduct.setSubclassId(teaSubclass.getId());
                teaProduct.setProductName(p.getProductName());
                teaProduct.setProductImg(p.getProductImg());
                teaProduct.setRemark(p.getProductImg());
                teaProduct.setProductPrice(p.getProductMoney() != null ? new BigDecimal(p.getProductMoney()) : null);
                teaProduct.setGuidePrice(p.getGuidePrice() != null ? new BigDecimal(p.getGuidePrice()) : null);
                teaProduct.setTradePrice(p.getTradePrice() != null ? new BigDecimal(p.getTradePrice()) : null);
                teaProduct.setProductFlagId(p.getProductId());
                teaProduct.setBiliPrice(p.getBili() != null ? new BigDecimal(p.getBili()) : null);
                if (StringUtils.isBlank(p.getBili()) || StringUtils.equals(p.getBili(), "0.0")) {
                    teaProduct.setBiliType("0"); // 0平，1涨，2降
                } else {
                    teaProduct.setBiliType(p.getType()); // 1涨，2降
                }
                teaProduct.setLabel(p.getLabel());
                teaProduct.setCreateTime(new Date());
                teaProduct.setUpdateTime(new Date());
                teaProducts.add(teaProduct);
            }
            if (teaProducts != null && teaProducts.size() > 0) {
                teaProductService.insertTeaProductAll(teaProducts);
                log.info("小类为{}的数据批量插入完成!", teaSubclass.getId());
            }
        }
    }

    private void updateProductSubclassBySubclass(TeaSubclass teaSubclass, List<ProductInfoRecord> records) {
        if (records != null && records.size() > 0) {
            List<TeaProduct> teaProducts = new ArrayList<>();
            for (ProductInfoRecord p : records) {
                TeaProduct teaProduct = new TeaProduct();
                teaProduct.setSubclassId(teaSubclass.getId());
                teaProduct.setProductFlagId(p.getProductId());
                teaProducts.add(teaProduct);
            }
            if (teaProducts != null && teaProducts.size() > 0) {
                teaProductService.updateProductSubclassIdAll(teaProducts);
                log.info("小类为{}的数据批量修改小类id完成!", teaSubclass.getId());
            }
        }
    }

    /**
     * 根据产品id拉取产品历史明细信息
     *
     * @return
     */
    @GetMapping("/pullProductDetails")
    public AjaxResult pullProductDetailById() throws ParseException, InterruptedException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTY4NjE3NDAsInVzZXJuYW1lIjoiMTU1MTc1Mjg3MjkifQ.UTt2E3-LYYssHUsGv9JXI113OAnr2Vl1ITNnMG3g6GA";
        String url = "https://www.yanjiuchacha.com/api/market/atProductMoney/useTheNewQueryList?productId={productId}&startDay={startDay}&endDay={endDay}";
        List<TeaProduct> teaProducts = teaProductService.selectTeaProductList(null);
        List<String> errorUrlList = new ArrayList<>();

        if (teaProducts != null && teaProducts.size() > 0) {
            for (TeaProduct teaProduct : teaProducts) {
                String urlTemp = url.replace("{productId}", teaProduct.getProductFlagId()); // 产品id
                urlTemp = urlTemp.replace("{startDay}", "2021-06-12"); // 开始时间
                urlTemp = urlTemp.replace("{endDay}", "2024-06-13"); // 结束时间
                PullProductDetailInfo pullProductDetailInfo = (PullProductDetailInfo) pullDataUtil.getMethodPull(urlTemp, token, PullProductDetailInfo.class);
                if (pullProductDetailInfo != null) {
                    if (pullProductDetailInfo.getCode() == 200) {
                        List<PullProductDetailResult> result = pullProductDetailInfo.getResult();
                        List<TeaPriceDetails> list = new ArrayList<>();
                        if (result != null && result.size() > 0) {
                            for (PullProductDetailResult dr : result) {
                                TeaPriceDetails teaPriceDetails = new TeaPriceDetails();
                                teaPriceDetails.setProductId(teaProduct.getId());
                                teaPriceDetails.setProductPrice(dr.getMoney() != null ? new BigDecimal(dr.getMoney()) : null);
                                teaPriceDetails.setGuidePrice(dr.getGuidePrice() != null ? new BigDecimal(dr.getGuidePrice()) : null);
                                teaPriceDetails.setTradePrice(dr.getTradePrice() != null ? new BigDecimal(dr.getTradePrice()) : null);
                                teaPriceDetails.setBiliPrice(dr.getBili() != null ? new BigDecimal(dr.getBili()) : null);
                                teaPriceDetails.setCreateTime(sdf.parse(dr.getMoneyDay()));
                                if (StringUtils.isBlank(dr.getBili()) || StringUtils.equals(dr.getBili(), "0.0")) {
                                    teaPriceDetails.setBiliType("0"); // 0平，1涨，2降
                                } else {
                                    teaPriceDetails.setBiliType(dr.getType()); // 1涨，2降
                                }
                                teaPriceDetails.setUpdateTime(new Date());
                                list.add(teaPriceDetails);
                            }
                        }
                        if (list != null && list.size() > 0) {
                            teaPriceDetailsService.insertTeaPriceDetailsAll(list);
                            log.info("产品ID为{}的历史明细批量插入成功!", teaProduct.getProductFlagId());
                        }
                        log.info("获取到数据为：{}", result);
                    } else {
                        errorUrlList.add(urlTemp);
                        log.info("请求错误，记录并等待10s请求下一条！");
                        Thread.sleep(10000);
                    }
                } else {
                    errorUrlList.add(urlTemp);
                    log.info("请求结果为空，记录并等待10s请求下一条！");
                    Thread.sleep(10000);
                }
            }
        }
        if (errorUrlList != null && errorUrlList.size() > 0) {
            log.info("失败获取明细的条数为: {}", errorUrlList.size());
            this.saveErrorUrlToFile("/home/sophia/application/teaWine/pullData/detailsErrorUrl.txt", errorUrlList);
        }
        return AjaxResult.success();
    }

    private void saveErrorUrlToFile(String fileName, List<String> list) {
        try {
            Files.write(Paths.get(fileName), list);
            log.info("文件{} 写入成功", fileName);
        } catch (IOException e) {
            log.error("写入文件时出现异常：{}", e.getMessage());
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 重新拉取文件中的数据
     *
     * @param type 1:产品信息; 2:产品明细信息; 3:产品小类id更新
     * @return
     */
    @GetMapping("/rePullData/{type}/{fileName}")
    public AjaxResult rePullData(@PathVariable("type") String type, @PathVariable("fileName") String fileName) throws InterruptedException, ParseException {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3MTY4NjE3NDAsInVzZXJuYW1lIjoiMTU1MTc1Mjg3MjkifQ.UTt2E3-LYYssHUsGv9JXI113OAnr2Vl1ITNnMG3g6GA";
        String filePath = "/home/sophia/application/teaWine/pullData/{fileFlag}.txt";
        // String str = "https://www.yanjiuchacha.com/api/market/atProduct/useTheNewQueryPageList?pageNo=5&pageSize=15&productType=1&manufacturerId=1456195239966027778&productName=&";

        String fileInfo = filePath.replace("{fileFlag}", fileName);
        List<String> urlList = this.readFileToList(fileInfo);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());
        List<String> errorUrlList = new ArrayList<>();
        if (StringUtils.equals(type, "1")) {
            // 拉取产品数据
            for (String url : urlList) {
                String targetStr = "manufacturerId=";
                String targetStr1 = "&productName=&";
                int i = url.indexOf(targetStr);
                String subId = url.substring(i + targetStr.length()).replace(targetStr1, "");
                log.info("小类id：{}", subId);
                TeaSubclass tsc = new TeaSubclass();
                tsc.setRemark(subId);
                List<TeaSubclass> list = teaSubclassService.selectTeaSubclassList(tsc);
                TeaSubclass teaSubclass = list.get(0);
                // 第一次请求
                PullProductInfo pullProductInfo = (PullProductInfo) pullDataUtil.getMethodPull(url, token, PullProductInfo.class);
                if (pullProductInfo != null) {
                    if (pullProductInfo.getCode() == 200) {
                        ProductInfoResult result = pullProductInfo.getResult();
                        if (result != null) {
                            log.info("批量插入到产品表...");
                            insertProductBySubclass(teaSubclass, result.getRecords());
                        }
                    } else {
                        errorUrlList.add(url);
                        log.info("请求状态码为500，记录并等待10s请求下一条！");
                        Thread.sleep(10000);
                    }
                }
            }
            if (errorUrlList != null && errorUrlList.size() > 0) {
                log.info("失败获取产品信息的条数为: {}", errorUrlList.size());
                this.saveErrorUrlToFile("/home/sophia/application/teaWine/pullData/productsErrorUrl" + time + ".txt", errorUrlList);
            }
        } else if (StringUtils.equals(type, "2")) {
            // String url = "https://www.yanjiuchacha.com/api/market/atProductMoney/useTheNewQueryList?productId={productId}&startDay={startDay}&endDay={endDay}";
            // 拉取明细数据
            for (String url : urlList) {
                String targetStr = "productId=";
                String targetStr1 = "&startDay=";
                int i = url.indexOf(targetStr);
                int e = url.indexOf(targetStr1);
                String productId = url.substring(i + targetStr.length(), e);
                log.info("产品id：{}", productId);
                TeaProduct teaProduct = teaProductService.selectTeaProductByFlagId(productId);
                PullProductDetailInfo pullProductDetailInfo = (PullProductDetailInfo) pullDataUtil.getMethodPull(url, token, PullProductDetailInfo.class);
                if (pullProductDetailInfo != null) {
                    if (pullProductDetailInfo.getCode() == 200) {
                        List<PullProductDetailResult> result = pullProductDetailInfo.getResult();
                        List<TeaPriceDetails> list = new ArrayList<>();
                        if (result != null && result.size() > 0) {
                            for (PullProductDetailResult dr : result) {
                                TeaPriceDetails teaPriceDetails = new TeaPriceDetails();
                                teaPriceDetails.setProductId(teaProduct.getId());
                                teaPriceDetails.setProductPrice(dr.getMoney() != null ? new BigDecimal(dr.getMoney()) : null);
                                teaPriceDetails.setGuidePrice(dr.getGuidePrice() != null ? new BigDecimal(dr.getGuidePrice()) : null);
                                teaPriceDetails.setTradePrice(dr.getTradePrice() != null ? new BigDecimal(dr.getTradePrice()) : null);
                                teaPriceDetails.setBiliPrice(dr.getBili() != null ? new BigDecimal(dr.getBili()) : null);
                                teaPriceDetails.setCreateTime(sdf1.parse(dr.getMoneyDay()));
                                if (StringUtils.isBlank(dr.getBili()) || StringUtils.equals(dr.getBili(), "0.0")) {
                                    teaPriceDetails.setBiliType("0"); // 0平，1涨，2降
                                } else {
                                    teaPriceDetails.setBiliType(dr.getType()); // 1涨，2降
                                }
                                teaPriceDetails.setUpdateTime(new Date());
                                list.add(teaPriceDetails);
                            }
                        }
                        if (list != null && list.size() > 0) {
                            teaPriceDetailsService.insertTeaPriceDetailsAll(list);
                            log.info("产品ID为{}的历史明细批量插入成功!", teaProduct.getProductFlagId());
                        }
                        log.info("获取到数据为：{}", result);
                    } else {
                        errorUrlList.add(url);
                        log.info("请求错误，记录并等待10s请求下一条！");
                        Thread.sleep(10000);
                    }
                }
            }
            if (errorUrlList != null && errorUrlList.size() > 0) {
                log.info("失败获取产品信息的条数为: {}", errorUrlList.size());
                this.saveErrorUrlToFile("/home/sophia/application/teaWine/pullData/detailsErrorUrl" + time + ".txt", errorUrlList);
            }
        } else if (StringUtils.equals(type, "3")) {
            // 拉取产品数据
            for (String url : urlList) {
                String targetStr = "manufacturerId=";
                String targetStr1 = "&productName=&";
                int i = url.indexOf(targetStr);
                String subId = url.substring(i + targetStr.length()).replace(targetStr1, "");
                log.info("小类id：{}", subId);
                TeaSubclass tsc = new TeaSubclass();
                tsc.setRemark(subId);
                List<TeaSubclass> list = teaSubclassService.selectTeaSubclassList(tsc);
                TeaSubclass teaSubclass = list.get(0);
                // 第一次请求
                PullProductInfo pullProductInfo = (PullProductInfo) pullDataUtil.getMethodPull(url, token, PullProductInfo.class);
                if (pullProductInfo != null) {
                    if (pullProductInfo.getCode() == 200) {
                        ProductInfoResult result = pullProductInfo.getResult();
                        if (result != null) {
                            log.info("批量插入到产品表...");
                            this.updateProductSubclassBySubclass(teaSubclass, result.getRecords());
                        }
                    } else {
                        errorUrlList.add(url);
                        log.info("请求状态码为500，记录并等待10s请求下一条！");
                        Thread.sleep(10000);
                    }
                }
            }
            if (errorUrlList != null && errorUrlList.size() > 0) {
                log.info("失败获取产品信息的条数为: {}", errorUrlList.size());
                this.saveErrorUrlToFile("/home/sophia/application/teaWine/pullData/productsErrorUrl" + time + ".txt", errorUrlList);
            }
        }
        return AjaxResult.success();
    }

    private List<String> readFileToList(String filePath) {
        List<String> list = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                list.add(line);
            }
        } catch (Exception e) {
            log.error("文件转list 出现异常：{}", e.getMessage());
            log.error(e.getMessage(), e);
        }
        return list;
    }


    /**
     * 根据报文解析并更新产品信息
     *
     * @param str json报文信息
     * @return
     */
    @PutMapping("/updateProductByStr")
    public AjaxResult updateProductByStr(@RequestBody String str) {
        try {
            Date date = new Date(); // 获取当前时间
            log.info("手动更新产品数据...");
            if (StringUtils.isBlank(str)) {
                log.info("入参为空...");
                return AjaxResult.error("入参不能为空");
            }
            List<ProductInfo> pList = new ArrayList<>();
            // 处理JSON数据
            JSONObject jsonObject = JSONUtil.parseObj(str);
            JSONObject result = jsonObject.getJSONObject("result");
            Root root = JSONUtil.toBean(result, Root.class);
            List<ProductInfo> records = root.getRecords();
            log.info("信息:{}", records);
            pList.addAll(records);
            List<TeaProduct> teaProducts = new ArrayList<>();
            // 设置数据并更新入库
            if (pList != null && pList.size() > 0) {
                for (ProductInfo p : pList) {
                    TeaProduct teaProduct = new TeaProduct();
                    teaProduct.setProductPrice(p.getProductMoney() != null ? new BigDecimal(p.getProductMoney()) : null);
                    teaProduct.setGuidePrice(p.getGuidePrice() != null ? new BigDecimal(p.getGuidePrice()) : null);
                    teaProduct.setTradePrice(p.getTradePrice() != null ? new BigDecimal(p.getTradePrice()) : null);
                    teaProduct.setBiliPrice(p.getBili() != null ? new BigDecimal(p.getBili()) : null);
                    if (StringUtils.isBlank(p.getBili()) || StringUtils.equals(p.getBili(), "0.0")) {
                        teaProduct.setBiliType("0"); // 0平，1涨，2降
                    } else {
                        teaProduct.setBiliType(p.getType()); // 1涨，2降
                    }
                    teaProduct.setProductFlagId(p.getProductId());
                    teaProduct.setCreateTime(date);
                    teaProduct.setUpdateTime(new Date());
                    teaProducts.add(teaProduct);
                }
            }
            if (teaProducts != null && teaProducts.size() > 0) {
                teaProductService.updateTeaProductAllByFlagId(teaProducts);
                log.info("数据批量修改完成!");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("json解析入库出现异常：{}", e.getMessage());
            log.error(e.getMessage(), e);
            return AjaxResult.error("json解析入库出现异常");
        }
    }

    /**
     * 根据文件解析报文信息
     *
     * @return
     */
    @PostMapping("/updateProductByFile")
    public AjaxResult updateProductByFile(MultipartFile multFile) {
        try {
            Date date = new Date(); // 获取当前时间
            // 创建Reader对象
            SAXReader reader = new SAXReader();
            File file = FileUtils.multipartFileToFile(multFile);
            // 加载xml
            Document document = reader.read(file);
            // 获取根节点
            Element rootElement = document.getRootElement();
            Iterator iterator = rootElement.elementIterator();
            List<String> list = new ArrayList<>();

            while (iterator.hasNext()) {
                Element stu = (Element) iterator.next();
                List<Attribute> attributes = stu.attributes();
                log.info("======获取属性值======");
                for (Attribute attribute : attributes) {
                    log.info(attribute.getValue());
                }
                log.info("======遍历子节点======");
                Iterator iterator1 = stu.elementIterator();
                while (iterator1.hasNext()) {
                    Element stuChild = (Element) iterator1.next();
                    log.info("节点名：" + stuChild.getName() + "---节点值：" + stuChild.getStringValue());
                    if (StringUtils.equals(stuChild.getName(), "response")) {
                        list.add(stuChild.getStringValue());
                    }
                }
            }
            List<String> jsonList = new ArrayList<>();
            // Base64解析
            if (list != null && list.size() > 0) {
                for (String s : list) {
                    byte[] decode = Base64.getDecoder().decode(s);
                    String resp = new String(decode);
                    String start = "{\"success\"";
                    // 获取开始子串的索引
                    int index = resp.indexOf(start);
                    if (index != -1) {
                        // 从开始子串结束的位置开始截取，直到字符串末尾
                        resp = start + resp.substring(index + start.length());
                    }
                    jsonList.add(resp);
                }
            }
            List<ProductInfo> pList = new ArrayList<>();
            // 处理JSON数据
            if (jsonList != null && jsonList.size() > 0) {
                for (String json : jsonList) {
                    JSONObject jsonObject = JSONUtil.parseObj(json);
                    JSONObject result = jsonObject.getJSONObject("result");
                    Root root = JSONUtil.toBean(result, Root.class);
                    List<ProductInfo> records = root.getRecords();
                    log.info("信息:{}", records);
                    pList.addAll(records);
                }
            }
            List<TeaProduct> teaProducts = new ArrayList<>();
            // 设置数据并更新入库
            if (pList != null && pList.size() > 0) {
                for (ProductInfo p : pList) {
                    TeaProduct teaProduct = new TeaProduct();
                    teaProduct.setProductPrice(p.getProductMoney() != null ? new BigDecimal(p.getProductMoney()) : null);
                    teaProduct.setGuidePrice(p.getGuidePrice() != null ? new BigDecimal(p.getGuidePrice()) : null);
                    teaProduct.setTradePrice(p.getTradePrice() != null ? new BigDecimal(p.getTradePrice()) : null);
                    teaProduct.setBiliPrice(p.getBili() != null ? new BigDecimal(p.getBili()) : null);
                    if (StringUtils.isBlank(p.getBili()) || StringUtils.equals(p.getBili(), "0.0")) {
                        teaProduct.setBiliType("0"); // 0平，1涨，2降
                    } else {
                        teaProduct.setBiliType(p.getType()); // 1涨，2降
                    }
                    teaProduct.setProductFlagId(p.getProductId());
                    teaProduct.setCreateTime(date);
                    teaProduct.setUpdateTime(new Date());
                    teaProducts.add(teaProduct);
                }
            }
            if (teaProducts != null && teaProducts.size() > 0) {
                teaProductService.updateTeaProductAllByFlagId(teaProducts);
                log.info("数据批量修改完成!");
            }
            return AjaxResult.success();
        } catch (Exception e) {
            log.error("文件解析入库出现异常：{}", e.getMessage());
            log.error(e.getMessage(), e);
            return AjaxResult.error("文件解析入库出现异常");
        }
    }

    /**
     * 入库产品数据
     *
     * @return
     * @throws DocumentException
     */
    @GetMapping("/dealProduct")
    public AjaxResult dealProduct() throws DocumentException {
        // 创建Reader对象
        SAXReader reader = new SAXReader();
        // 加载xml
        Document document = reader.read(new File("D:\\QuickStart\\Desktop\\小程序申请\\采集数据\\第一页酒水.xml"));
        // 获取根节点
        Element rootElement = document.getRootElement();
        Iterator iterator = rootElement.elementIterator();
        List<String> list = new ArrayList<>();

        while (iterator.hasNext()) {
            Element stu = (Element) iterator.next();
            List<Attribute> attributes = stu.attributes();
            System.out.println("======获取属性值======");
            for (Attribute attribute : attributes) {
                System.out.println(attribute.getValue());
            }
            System.out.println("======遍历子节点======");
            Iterator iterator1 = stu.elementIterator();
            while (iterator1.hasNext()) {
                Element stuChild = (Element) iterator1.next();
                System.out.println("节点名：" + stuChild.getName() + "---节点值：" + stuChild.getStringValue());
                if (StringUtils.equals(stuChild.getName(), "response")) {
                    list.add(stuChild.getStringValue());
                }
            }
        }
        List<String> jsonList = new ArrayList<>();
        // Base64解析
        if (list != null && list.size() > 0) {
            for (String s : list) {
                byte[] decode = Base64.getDecoder().decode(s);
                String resp = new String(decode);
                String start = "{\"success\"";
                // 获取开始子串的索引
                int index = resp.indexOf(start);
                if (index != -1) {
                    // 从开始子串结束的位置开始截取，直到字符串末尾
                    resp = start + resp.substring(index + start.length());
                }
                jsonList.add(resp);
            }
        }
        List<ProductInfo> pList = new ArrayList<>();
        // 处理JSON数据
        if (jsonList != null && jsonList.size() > 0) {
            for (String json : jsonList) {
                JSONObject jsonObject = JSONUtil.parseObj(json);
                JSONObject result = jsonObject.getJSONObject("result");
                Root root = JSONUtil.toBean(result, Root.class);
                List<ProductInfo> records = root.getRecords();
                log.info("信息:{}", records);
                pList.addAll(records);
            }
        }
        List<TeaProduct> teaProducts = new ArrayList<>();
        // 设置数据并入库
        if (pList != null && pList.size() > 0) {
            for (ProductInfo p : pList) {
                TeaProduct teaProduct = new TeaProduct();
                teaProduct.setProductName(p.getProductName());
                teaProduct.setProductImg(p.getProductImg());
                teaProduct.setProductPrice(p.getProductMoney() != null ? new BigDecimal(p.getProductMoney()) : null);
                teaProduct.setGuidePrice(p.getGuidePrice() != null ? new BigDecimal(p.getGuidePrice()) : null);
                teaProduct.setTradePrice(p.getTradePrice() != null ? new BigDecimal(p.getTradePrice()) : null);
                teaProduct.setCategoryId(2l);
                teaProduct.setProductFlagId(p.getProductId());
                teaProduct.setBiliPrice(p.getBili() != null ? new BigDecimal(p.getBili()) : null);
                if (StringUtils.isBlank(p.getBili()) || StringUtils.equals(p.getBili(), "0.0")) {
                    teaProduct.setBiliType("0"); // 0平，1涨，2降
                } else {
                    teaProduct.setBiliType(p.getType()); // 1涨，2降
                }
                teaProduct.setLabel(p.getLabel());
                teaProduct.setCreateTime(new Date());
                teaProduct.setUpdateTime(new Date());
                teaProducts.add(teaProduct);
            }
        }

        if (teaProducts != null && teaProducts.size() > 0) {
            teaProductService.insertTeaProductAll(teaProducts);
            log.info("数据插入完成!");
        }
        return AjaxResult.success();

    }

    /**
     * 入库产品小类别数据
     *
     * @return
     * @throws DocumentException
     */
    @GetMapping("/dealProductSubClass")
    public AjaxResult dealProductSubClass() throws DocumentException {
        // 创建Reader对象
        SAXReader reader = new SAXReader();
        File dirs = new File("D:\\QuickStart\\Desktop\\小程序申请\\采集数据\\品牌分类酒水");
        File[] files = dirs.listFiles();
        HashMap<String, List<String>> map = Maps.newHashMap();

        if (files != null && files.length > 0) {
            for (File file : files) {
                String fileName = file.getName();
                String provinceName = fileName.replace("酒水数据.xml", "");
                Document document = reader.read(file);
                // 获取根节点
                Element rootElement = document.getRootElement();
                Iterator iterator = rootElement.elementIterator();
                List<String> list = new ArrayList<>();
                while (iterator.hasNext()) {
                    Element stu = (Element) iterator.next();
                    List<Attribute> attributes = stu.attributes();
                    System.out.println("======获取属性值======");
                    for (Attribute attribute : attributes) {
                        System.out.println(attribute.getValue());
                    }
                    System.out.println("======遍历子节点======");
                    Iterator iterator1 = stu.elementIterator();
                    while (iterator1.hasNext()) {
                        Element stuChild = (Element) iterator1.next();
                        System.out.println("节点名：" + stuChild.getName() + "---节点值：" + stuChild.getStringValue());
                        if (StringUtils.equals(stuChild.getName(), "response")) {
                            String value = stuChild.getStringValue();
                            byte[] decode = Base64.getDecoder().decode(value);
                            String resp = new String(decode);
                            String start = "{\"success\"";
                            // 获取开始子串的索引
                            int index = resp.indexOf(start);
                            if (index != -1) {
                                // 从开始子串结束的位置开始截取，直到字符串末尾
                                resp = start + resp.substring(index + start.length());
                            }
                            // 解析并处理
                            JSONObject jsonObject = JSONUtil.parseObj(resp);
                            JSONObject result = jsonObject.getJSONObject("result");
                            Root root = JSONUtil.toBean(result, Root.class);
                            List<ProductInfo> records = root.getRecords();
                            log.info("信息:{}", records);
                            // 查询地市小类数据
                            TeaSubclass tsc = teaSubclassService.selectTeaSubclassByName(provinceName);
                            for (ProductInfo record : records) {
                                teaProductService.updateProductSubclassId(tsc.getId(), record.getProductId());
                            }

                        }
                    }
                }
                map.put(provinceName, list);
            }
        }
        // 加载xml
        /*Document document = reader.read(new File("E:\\QuickStart\\DeskTop\\GCHTemp\\百货行情\\采集数据\\查询香烟所有数据.xml"));
        // 获取根节点
        Element rootElement = document.getRootElement();
        Iterator iterator = rootElement.elementIterator();
        List<String> list = new ArrayList<>();

        while (iterator.hasNext()) {
            Element stu = (Element) iterator.next();
            List<Attribute> attributes = stu.attributes();
            System.out.println("======获取属性值======");
            for (Attribute attribute : attributes) {
                System.out.println(attribute.getValue());
            }
            System.out.println("======遍历子节点======");
            Iterator iterator1 = stu.elementIterator();
            while (iterator1.hasNext()) {
                Element stuChild = (Element) iterator1.next();
                System.out.println("节点名：" + stuChild.getName() + "---节点值：" + stuChild.getStringValue());
                if (StringUtils.equals(stuChild.getName(), "response")) {
                    list.add(stuChild.getStringValue());
                }
            }
        }
        List<String> jsonList = new ArrayList<>();
        // Base64解析
        if (list != null && list.size() > 0) {
            for (String s : list) {
                byte[] decode = Base64.getDecoder().decode(s);
                String resp = new String(decode);
                String start = "{\"success\"";
                // 获取开始子串的索引
                int index = resp.indexOf(start);
                if (index != -1) {
                    // 从开始子串结束的位置开始截取，直到字符串末尾
                    resp = start + resp.substring(index + start.length());
                }
                jsonList.add(resp);
            }
        }
        List<ProductInfo> pList = new ArrayList<>();
        // 处理JSON数据
        if (jsonList != null && jsonList.size() > 0) {
            for (String json : jsonList) {
                JSONObject jsonObject = JSONUtil.parseObj(json);
                JSONObject result = jsonObject.getJSONObject("result");
                Root root = JSONUtil.toBean(result, Root.class);
                List<ProductInfo> records = root.getRecords();
                log.info("信息:{}", records);
                pList.addAll(records);
            }
        }
        List<TeaProduct> teaProducts = new ArrayList<>();
        // 设置数据并入库
        if (pList != null && pList.size() > 0) {
            for (ProductInfo p : pList) {
                TeaProduct teaProduct = new TeaProduct();
                teaProduct.setProductName(p.getProductName());
                teaProduct.setProductImg(p.getProductImg());
                teaProduct.setProductPrice(p.getProductMoney() != null ? new BigDecimal(p.getProductMoney()) : null);
                teaProduct.setGuidePrice(p.getGuidePrice() != null ? new BigDecimal(p.getGuidePrice()) : null);
                teaProduct.setTradePrice(p.getTradePrice() != null ? new BigDecimal(p.getTradePrice()) : null);
                teaProduct.setCategoryId(1l);
                teaProduct.setProductFlagId(p.getProductId());
                teaProduct.setBiliPrice(p.getBili() != null ? new BigDecimal(p.getBili()) : null);
                teaProduct.setBiliType(p.getType());
                teaProduct.setLabel(p.getLabel());
                teaProduct.setCreateTime(new Date());
                teaProduct.setUpdateTime(new Date());
                teaProducts.add(teaProduct);
            }
        }

        if (teaProducts != null && teaProducts.size() > 0) {
            teaProductService.insertTeaProductAll(teaProducts);
            log.info("数据插入完成!");
        }*/
        return AjaxResult.success();

    }

    /**
     * 入库产品明细数据
     *
     * @return
     * @throws DocumentException
     */
    @GetMapping("/dealProductDetail")
    public AjaxResult dealProductDetail() throws DocumentException, ParseException {
        // 创建Reader对
        SAXReader reader = new SAXReader();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        File file = new File("D:\\QuickStart\\Desktop\\小程序申请\\采集数据\\酒水明细(到飞天43度).xml");
        String fileName = file.getName();
        Document document = reader.read(file);
        // 获取根节点
        Element rootElement = document.getRootElement();
        Iterator iterator = rootElement.elementIterator();
        List<String> list = new ArrayList<>();
        while (iterator.hasNext()) {
            Element stu = (Element) iterator.next();
            List<Attribute> attributes = stu.attributes();
            System.out.println("======获取属性值======");
            for (Attribute attribute : attributes) {
                System.out.println(attribute.getValue());
            }
            System.out.println("======遍历子节点======");
            Iterator iterator1 = stu.elementIterator();
            String productId = "";
            while (iterator1.hasNext()) {
                Element stuChild = (Element) iterator1.next();
                System.out.println("节点名：" + stuChild.getName() + "---节点值：" + stuChild.getStringValue());
                if (StringUtils.equals(stuChild.getName(), "path")) {
                    String resp = stuChild.getStringValue();
                    String start = "productId=";
                    String end = "&startDay=";
                    log.info("截取前：{}", resp);
                    // 获取开始子串的索引
                    int index = resp.indexOf(start);
                    int endIndex = resp.indexOf(end);
                    if (index != -1 && endIndex != -1) {
                        // 从开始子串结束的位置开始截取，直到字符串末尾
                        resp = resp.substring(index + start.length(), endIndex);
                    }
                    log.info("截取后：{}", resp);
                    productId = resp;
                }
                if (StringUtils.equals(stuChild.getName(), "response") && StringUtils.isNotBlank(productId)) {
                    List<TeaPriceDetails> dList = new ArrayList<>();
                    TeaProduct teaProduct = teaProductService.selectTeaProductByFlagId(productId);
                    TeaPriceDetails details = teaPriceDetailsService.selectTeaPriceDetailsByProductId(teaProduct.getId());
                    if (details == null) {
                        String value = stuChild.getStringValue();
                        byte[] decode = Base64.getDecoder().decode(value);
                        String resp = new String(decode);
                        String start = "{\"success\"";
                        // 获取开始子串的索引
                        int index = resp.indexOf(start);
                        if (index != -1) {
                            // 从开始子串结束的位置开始截取，直到字符串末尾
                            resp = start + resp.substring(index + start.length());
                        }
                        // 解析并处理
                        JSONObject jsonObject = JSONUtil.parseObj(resp);
                        Root root = JSONUtil.toBean(jsonObject, Root.class);
                        List<VenderDomain> records = root.getResult();
                        log.info("信息:{}", records);
                        for (VenderDomain record : records) {
                            TeaPriceDetails teaPriceDetails = new TeaPriceDetails();
                            teaPriceDetails.setProductId(teaProduct.getId());
                            teaPriceDetails.setProductPrice(record.getMoney() != null ? new BigDecimal(record.getMoney()) : null);
                            teaPriceDetails.setGuidePrice(record.getMoney() != null ? new BigDecimal(record.getMoney()) : null);
                            teaPriceDetails.setTradePrice(record.getMoney() != null ? new BigDecimal(record.getMoney()) : null);
                            teaPriceDetails.setBiliPrice(record.getBili() != null ? new BigDecimal(record.getBili()) : null);
                            teaPriceDetails.setCreateTime(sdf.parse(record.getMoneyDay()));
                            if (StringUtils.isBlank(record.getBili()) || StringUtils.equals(record.getBili(), "0.0")) {
                                teaPriceDetails.setBiliType("0"); // 0平，1涨，2降
                            } else {
                                teaPriceDetails.setBiliType(record.getType()); // 1涨，2降
                            }
                            teaPriceDetails.setUpdateTime(new Date());
                            dList.add(teaPriceDetails);
                        }
                        if (dList != null && dList.size() > 0) {
                            teaPriceDetailsService.insertTeaPriceDetailsAll(dList);
                            log.info("产品ID为{}的历史明细批量插入成功!", productId);
                        }
                    } else {
                        log.info("产品ID为{}的历史明细已导入！", productId);
                    }
                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * 下载产品图片
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getProductImg/{categoryId}/{day}")
    public AjaxResult getProductImg(@PathVariable("categoryId") Long categoryId, @PathVariable("day") String day) throws Exception {
        log.info("拉取大类为：{}的产品图片信息...", categoryId);
        String sufUrl = "/profile/upload/2024/06/" + day + "/";
        String urlTemp = "https://www.yanjiuchacha.com/api/";
        TeaProduct tp = new TeaProduct();
        tp.setCategoryId(categoryId);
        List<TeaProduct> teaProducts = teaProductService.selectTeaProductList(tp);
        List<TeaProduct> list = new ArrayList<>();

        List<TeaProduct> result = new ArrayList<>();

        if (teaProducts != null && teaProducts.size() > 0) {
            for (TeaProduct teaProduct : teaProducts) {
                String uuid = UUID.randomUUID().toString().replace("-", "");
                String productImgUrl = urlTemp + URLEncoder.encode(teaProduct.getProductImg(), "UTF-8");
                String url = productImgUrl;
                String fileExtension = FileUtils.getFileExtension(productImgUrl);
                String destFilename = "/home/sophia/application/teaWine/pullData/upload/2024/06/" + day + "/" + uuid + fileExtension;
                try {
                    this.saveImage(url, destFilename);
                    log.info("图片成功保存至{}", destFilename);
                    teaProduct.setProductImg(sufUrl + uuid + fileExtension);
                    result.add(teaProduct);
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                    log.error("图片保存失败：{}", e.getMessage());
                    list.add(teaProduct);
                }
            }
        }
        log.info("处理转存失败的图片信息.....");
        if (list != null && list.size() > 0) {
            for (TeaProduct teaProduct : list) {
                String uuid = UUID.randomUUID().toString().replace("-", "");
                String productImgUrl = urlTemp + teaProduct.getProductImg();
                String url = productImgUrl;
                String fileExtension = FileUtils.getFileExtension(productImgUrl);
                String destFilename = "/home/sophia/application/teaWine/pullData/upload/2024/06/" + day + "/" + uuid + fileExtension;
                this.saveImage(url, destFilename);
                teaProduct.setProductImg(sufUrl + uuid + fileExtension);
                result.add(teaProduct);
            }
        }
        if (result != null && result.size() > 0) {
            teaProductService.updateTeaProductAll(result);
            log.info("修改产品图片共{}条", result.size());
        }
        return AjaxResult.success();
    }

    public void saveImage(String imageUrl, String destinationFile) throws Exception {
        URL url = new URL(imageUrl);
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        FileOutputStream outputStream = new FileOutputStream(destinationFile);

        byte[] buffer = new byte[1024];
        int bytesRead;

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        inputStream.close();
        outputStream.close();
        // System.out.println("图片成功保存至：" + destinationFile);
    }

    public static void download(String sourceURL, String destFilename) throws Exception {
        // 构造URL
        URL url = new URL(sourceURL);
        URLConnection con = url.openConnection();
        con.setConnectTimeout(3 * 1000);

        // 输入流
        InputStream is = con.getInputStream();
        byte[] bs = new byte[1024];
        int len;

        // 输出流
        File file = new File(destFilename);
        OutputStream os = new FileOutputStream(file);
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }

        // 关闭链接
        os.close();
        is.close();
    }


    public static void main(String[] args) throws Exception {
        // https://www.yanjiuchacha.com/api/market/atProduct/useTheNewQueryPageList?pageNo=5&pageSize=15&productType=1&manufacturerId=1456195239966027778&productName=&

        String str = "https://www.yanjiuchacha.com/api/market/atProductMoney/useTheNewQueryList?productId=1456195239966027778&startDay={startDay}&endDay={endDay}";
        // String str = "https://www.yanjiuchacha.com/api/market/atProduct/useTheNewQueryPageList?pageNo=5&pageSize=15&productType=1&manufacturerId=1456195239966027778&productName=&";
        String targetStr = "productId=";
        String targetStr1 = "&startDay=";
        int i = str.indexOf(targetStr);
        int e = str.indexOf(targetStr1);
        String result = str.substring(i + targetStr.length(), e);
        log.info(result);

        // List<String> list = Arrays.asList("第一行", "第二行", "第三行");
        // Files.write(Paths.get("E:\\QuickStart\\DeskTop\\GCHTemp\\查酒茶\\采集数据\\test.txt"), list);
        //log.info("文件{} 写入成功", fileName);
        /*String sourceURL = "https://www.yanjiuchacha.com/api/8ce7e4b4b5d8409ba2860f93018c51021_%E8%87%AA%E5%AE%9A%E4%B9%89px_2021-12-09+15_16_34.png";

        String url = URLDecoder.decode(sourceURL, "UTF-8");
        String destFilename = "D:\\teawine\\"
                + url.substring(url.lastIndexOf("/") + 1, url.length());

        download(sourceURL, destFilename);*/
        
        /*String resp = "/api/market/atProductMoney/useTheNewQueryList?productId=1466281361064734721&startDay=2021-05-26&endDay=2024-05-25";
        String start = "productId=";
        String end = "&startDay=";
        log.info("解码前：{}", resp);
        // 获取开始子串的索引
        int index = resp.indexOf(start);
        int endIndex = resp.indexOf(end);
        if (index != -1 && endIndex != -1) {
            // 从开始子串结束的位置开始截取，直到字符串末尾
            resp = resp.substring(index + start.length(), endIndex);
        }
        log.info("解码后：{}", resp);*/
        
        /*String str = "SFRUUC8xLjEgMjAwIA0KU2VydmVyOiBuZ2lueA0KRGF0ZTogU2F0LCAyNSBNYXkgMjAyNCAwOTo1MTozMyBHTVQNCkNvbnRlbnQtVHlwZTogYXBwbGljYXRpb24vanNvbg0KQ29ubmVjd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iwicHJvZHVjdE1vbmV5IjoiNjIzLjAiLCJwcm9kdWN0SW1nIjoiODY2MWI5MDlmYzUzNGMzZDkxNWJkNTI1M2Y2Yzk2ZmbkuK3ljY7vvIjova/vvIkucG5nIiwiZ3VpZGVQcmljZSI6NTgzLjAsImJpbGkiOiIyLjAiLCJpc0NvbGxlY3RJZCI6MCwibGFiZWwiOm51bGwsInR5cGUiOiIyIiwidHJhZGVQcmljZSI6NzAwLjAsInByb2R1Y3ROYW1lIjoi5Lit5Y2O77yI6L2v77yJMzI5In0seyJwcm9kdWN0SWQiOiIxNDY2MDk0MDg2NDMxMzIyMTEzIiwiem1vbmV5IjoiMTY1LjAiLCJwcm9kdWN0TW9uZXkiOiIxNjUuMCIsInByb2R1Y3RJbWciOiJmZjhkMTkwOGMxMGM0ZDQ4YTRiOWVkZGI0NjE1ZmI5OeeJoeS4ue+8iOi9r++8iS5wbmciLCJndWlkZVByaWNlIjoxMTQuNSwiYmlsaSI6IjAuMCIsImlzQ29sbGVjdElkIjowLCJsYWJlbCI6bnVsbCwidHlwZSI6IjEiLCJ0cmFkZVByaWNlIjoxNTAuMCwicHJvZHVjdE5hbWUiOiLniaHkuLnvvIjova/vvIkifV0sInRvdGFsIjoxMTI3LCJzaXplIjoxNSwiY3VycmVudCI6MSwib3JkZXJzIjpbXSwib3B0aW1pemVDb3VudFNxbCI6dHJ1ZSwiaGl0Q291bnQiOmZhbHNlLCJjb3VudElkIjpudWxsLCJtYXhMaW1pdCI6bnVsbCwic2VhcmNoQ291bnQiOnRydWUsInBhZ2VzIjo3Nn0sInRpbWVzdGFtcCI6MTcxNjYzMDY5MzU4NX0=";
        log.info("解码前：{}", str);
        byte[] decode = Base64.getDecoder().decode(str);
        String s = new String(decode);
        log.info("解码后：{}", s);*/
    }
}
